package com.ai.controller.user;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.common.controller.BaseController;
import com.ai.common.model.Pagination;
import com.ai.common.model.Result;
import com.ai.common.model.vo.BackOperatorVO;
import com.ai.common.utils.lang.json.JsonUtils;
import com.ai.model.edu.cms.backoperator.Role;
import com.ai.model.edu.cms.backoperator.RoleMenu;
import com.ai.model.edu.cms.backoperator.dto.OperatorRoleDTO;
import com.ai.model.edu.cms.backoperator.dto.RoleDTO;
import com.ai.model.edu.cms.backoperator.dto.RoleMenuDTO;
import com.ai.model.edu.cms.backoperator.vo.OperatorRoleVO;
import com.ai.model.edu.cms.backoperator.vo.RoleVO;
import com.ai.service.cms.user.BackendOperatorService;
import com.ai.service.cms.user.OperatorRoleService;
import com.ai.service.cms.user.RoleMenuService;
import com.ai.service.cms.user.RoleService;

@Controller
public class RoleController extends BaseController {
    @Resource
    private RoleService roleService;

    @Resource
    private BackendOperatorService backendOperatorService;

    @Resource
    private RoleMenuService roleMenuService;

    @Resource
    private OperatorRoleService operatorRoleService;

    /**
     * 进入角色-菜单/权限管理
     * 
     * @return
     */
//    @AuthorityAnnotation(functionType = { FunctionType.UNICOM_BACKEND_ROLE_ROLE2MENU })
    @RequestMapping("/role/role2Menu")
    public String toRole2Menu(HttpServletRequest request, Long roleId) {

        List<RoleMenu> roleMenuLst = roleMenuService.selectByRoleId(roleId);
        StringBuffer menuIds = new StringBuffer();
        for (RoleMenu roleMenu : roleMenuLst) {
            menuIds.append(roleMenu.getMenuId()).append(",");
        }

        List<Long> ids = new ArrayList<Long>();
        ids.add(roleId);
        List<RoleVO> roleVOLst = roleService.findRoleVOsByIds(ids);
        String roleName = "";
        if (CollectionUtils.isNotEmpty(roleVOLst)) {
            roleName = roleVOLst.get(0).getRoleName();
        }

        request.setAttribute("menuIds", menuIds.toString());
        request.setAttribute("roleId", roleId);
        request.setAttribute("roleName", roleName);

        return "user/role2Menu";
    }

    /**
     * 进入角色-菜单/权限管理
     * 
     * @return
     */
//    @AuthorityAnnotation(functionType = { FunctionType.UNICOM_BACKEND_ROLE_SAVEROLE2MENU })
    @RequestMapping("/role/saveRole2Menu")
    @ResponseBody
    public Result saveRole2Menu(@RequestBody RoleMenuDTO roleMenuDTO, HttpServletRequest request) {
        Long operatorId = this.getBackendOperator(request).getId();
        String succMsg = roleMenuService.updateRoleMenu(roleMenuDTO, operatorId);
        return new Result(true, succMsg);
    }

    /**
     * Description ： 进入角色管理页面
     * 
     * nyj
     * 
     * @return
     * @since
     *
     */
    @RequestMapping("/user/toRolePage")
//    @AuthorityAnnotation(functionType = { FunctionType.ROLE_PAGE_FIND })
    public String toRolePage() {
        return "user/rolePage";
    }

    /**
     * Description ： 查找角色分页信息
     * 
     * nyj
     * 
     * @param roleDTO
     * @since
     *
     */
    @RequestMapping("/user/findRoleList")
    @ResponseBody
    public Pagination<RoleVO> findRoleList(@ModelAttribute RoleDTO roleDTO) {
        Pagination<RoleVO> pag = new Pagination<RoleVO>();
        pag = roleService.selectRolePagByDTO(roleDTO);
        return pag;
    }

    /**
     * Description ： 新增角色
     * 
     * nyj
     * 
     * @param roleDTO
     * @since
     *
     */
    @RequestMapping("/user/addRole")
    @ResponseBody
//    @AuthorityAnnotation(functionType = { FunctionType.ROLE_ADD })
    public Result addRole(@ModelAttribute Role role, HttpServletRequest req) {
        // 获取登录信息
        BackOperatorVO backOperatorVO = this.getBackendOperator(req);
        // 设置操作人信息
        role.setCreateBy(backOperatorVO.getId());
        role.setUpdateBy(backOperatorVO.getId());
        backendOperatorService.addRoleInfo(role);
        return new Result(true, Result.SUCCESS, "成功");
    }

    /**
     * Description ： 启用角色
     * 
     * nyj
     * 
     * @return
     * @since
     *
     */
    @RequestMapping("/user/enableRole")
    @ResponseBody
//    @AuthorityAnnotation(functionType = { FunctionType.ROLE_ENABLE })
    public Result enableRole(@ModelAttribute RoleDTO roleDTO, HttpServletRequest req) {
        // 获取登录信息
        BackOperatorVO backOperatorVO = this.getBackendOperator(req);
        // 设置操作人信息
        roleDTO.setUpdateBy(backOperatorVO.getId());
        int result = backendOperatorService.enableRole(roleDTO);
        if (result > 0) {
            return new Result(true, Result.SUCCESS, "成功");
        } else {
            return new Result(false, Result.INPUT, "失败");
        }
    }

    /**
     * Description ： 冻结角色
     * 
     * nyj
     * 
     * @return
     * @since
     *
     */
    @RequestMapping("/user/unableRole")
    @ResponseBody
//    @AuthorityAnnotation(functionType = { FunctionType.ROLE_UNABLE })
    public Result unableRole(@ModelAttribute RoleDTO roleDTO, HttpServletRequest req) {
        // 获取登录信息
        BackOperatorVO backOperatorVO = this.getBackendOperator(req);
        // 设置操作人信息
        roleDTO.setUpdateBy(backOperatorVO.getId());
        int result = backendOperatorService.unableRole(roleDTO);
        if (result > 0) {
            return new Result(true, Result.SUCCESS, "成功");
        } else {
            return new Result(false, Result.INPUT, "失败");
        }
    }

    /**
     * Description ： 新增用户/角色关系
     * 
     * nyj
     * 
     * @return
     * @since
     *
     */
    @RequestMapping("/user/addRole2User")
    @ResponseBody
//    @AuthorityAnnotation(functionType = { FunctionType.ADD_ROLE2USER })
    public Result addRole2User(String operatorRoleDTOJson, HttpServletRequest req) {
        // JSON转对象
        OperatorRoleDTO operatorRoleDTO = JsonUtils.fromJson(operatorRoleDTOJson, OperatorRoleDTO.class);
        // 获取登录信息
        BackOperatorVO backOperatorVO = this.getBackendOperator(req);
        // 设置操作人信息
        operatorRoleDTO.setCreateBy(backOperatorVO.getId());
        backendOperatorService.addRole2User(operatorRoleDTO);
        return new Result(true, Result.SUCCESS, "成功");
    }

    /**
     * Description ： 根据角色ID查找用户信息
     * 
     * nyj
     * 
     * @param roleId
     * @return
     * @since
     *
     */
    @RequestMapping("/user/findOperatorRoleByRoleId")
    @ResponseBody
    public List<OperatorRoleVO> findOperatorRoleByRoleId(Long roleId) {
        OperatorRoleDTO userRoleDTO = new OperatorRoleDTO();
        userRoleDTO.setRoleId(roleId);
        return operatorRoleService.findUserRoleVOsByDTO(userRoleDTO);
    }

    /**
     * Description ： 删除
     * 
     * nyj
     * 
     * @return
     * @since
     *
     */
    @RequestMapping("/user/deleteRoleById")
    @ResponseBody
//    @AuthorityAnnotation(functionType = { FunctionType.ROLE_DEL })
    public Result deleteRoleById(@ModelAttribute RoleDTO roleDTO, HttpServletRequest req) {
        // 获取登录信息
        BackOperatorVO backOperatorVO = this.getBackendOperator(req);
        // 设置操作人信息
        roleDTO.setUpdateBy(backOperatorVO.getId());
        int result = backendOperatorService.deleteRoleById(roleDTO);
        if (result > 0) {
            return new Result(true, Result.SUCCESS, "成功");
        } else {
            return new Result(false, Result.INPUT, "失败");
        }
    }
}
